package dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import po.EmpHistory;
import po.Employment;
import po.Order;
import po.User;

public class EmploymentDao extends BaseDAO implements IEmploymentDao{
	public List findByHql(String hql) {
		try {
			String queryString = hql;
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {		
			throw re;
		} 
	} 
	
	//传入id 搜索招聘信息
	public Object findEmpInfo(int id) {			
		try {
			Object obj = getSession().find(Employment.class, id);
			return obj;
		}catch (RuntimeException re) {
			//log.error("find by id failed", re);
			throw re;
		}
	}
	
	//传入用户id 搜索用户信息
	public Object findUserInfo(int id) {
			
		try {
			Object obj = getSession().find(User.class, id);
			return obj;
		}catch (RuntimeException re) {
			//log.error("find by id failed", re);
			throw re;
		}
	}	
	//更新
	public void updateStatus(Employment emp) {		
		Transaction tran = null;
		Session session = null;
		try {
			session = getSession();
			tran = session.beginTransaction();
			session.update(emp);
			tran.commit();
		} catch (RuntimeException re) {
			if(tran != null) tran.rollback();
			throw re;
		} finally {
			session.close();
		}
	}	
	//删除
	public void deleteEmp(Employment emp) {		
		Transaction tran = null;
		Session session = null;
		try {
			session = getSession();
			tran = session.beginTransaction();
			session.delete(emp);
			tran.commit();
		} catch (RuntimeException re) {
			if(tran != null) tran.rollback();
			throw re;
		} finally {
			session.close();
		}
	}	
	//添加到历史
	public void saveHistory(EmpHistory history) {		
		Transaction tran = null;
		Session session = null;
		try {
			session = getSession();
			tran = session.beginTransaction();
			session.save(history);   
			tran.commit();
		} catch (RuntimeException re) {
			if(tran != null) tran.rollback();
			throw re;
		} finally {
			session.close();
		}
	}	
}
